package com.models
{
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import flash.events.Event;
	import mx.controls.Button;
	import mx.controls.TextInput;
	import mx.controls.Label;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import com.data.ActivityDAO;
	import com.data.AccessRight;
	import com.data.LoginDAO;
	import com.container.AccessList;
	import mx.core.Application;
	import com.events.AccessEvent;
	
	public class Access extends UIComponent
	{
		private var _xmlData:XML;
		private var _pass:TextInput;
		private var _email:TextInput;
		private var _lbPass:Label;
		private var _lbEmail:Label;
		private var _btLogin:Button;
		private var _lbError:Label;
		private var _activities:Array = [];
		private var _login:LoginDAO;
		
		public function Access()
		{
			_btLogin = new Button();
			_btLogin.label = "Login";
			
			_pass = new TextInput();
			_email = new TextInput();
			
			_lbPass = new Label();
			_lbEmail = new Label();
			_lbError = new Label();
			
			_lbPass.width = 50;
			_lbPass.height = 20;
			
			_lbEmail.width = 50;
			_lbEmail.height = 20;
			_lbError.width = 200;
			_lbError.height = 20;
			_pass.width = 100;
			_pass.height = 20;
			_email.width = 100;
			_email.height = 20;
			_btLogin.width = 100;
			_btLogin.height = 20;
			_lbPass.text = "Pass:";
			_lbEmail.text = "Email:";
			_lbError.text = "";
			_lbPass.move( 50, 120);
			_lbEmail.move( 50, 100 );
			
			_email.move(100, 100);
			_pass.move(100, 120);
			_btLogin.move(100, 140);
			_lbError.move(100, 180);
			
			addChild(_btLogin);
			addChild(_pass);
			addChild(_email);
			addChild(_lbPass);
			addChild(_lbEmail);
			addChild(_lbError);
			
			_btLogin.addEventListener(FlexEvent.BUTTON_DOWN, loginHandler);
			
		}
		
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if(_login && _login.success)
			{
				removeChild(_btLogin);
				removeChild(_pass);
				removeChild(_email);
				removeChild(_lbPass);
				removeChild(_lbEmail);
				removeChild(_lbError);
			}
		}
		
		private function loginHandler(evt:FlexEvent):void
		{
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE, xmlHandler);
			loader.load( new URLRequest( "data/data.xml" ));
		}
		
		private function xmlHandler( evt:Event ):void
		{
			try
			{
				_xmlData = new XML( evt.target.data );
				login(_pass.text, _email.text);
			}
			catch(e:TypeError)
			{
				trace("Could not load XML");
				trace(e.message);
			}
			
		}
		
		private function login(pPass:String, pEmail:String):void
		{
			_login = new LoginDAO(pEmail, pPass);
			
			for each( var email:XML in _xmlData..access.*.( @email == pEmail.toLowerCase()))
			{
				if(email.@pass == pPass)
				{
					_login.success = true;
					var parent:XML = email.parent().parent();
					var activity:ActivityDAO = new ActivityDAO();
					activity.creator = parent.@creator;
					activity.message = parent.message;
					activity.dataId = parent.@id;
					activity.text = parent.@label;
					var accessRight:String = email.name();
					var show:Boolean = false;
					switch(accessRight)
					{
						case AccessRight.FULL:
							activity.accessRight = AccessRight.FULL;
							show = showActivity(activity.dataId);
							break;
						case AccessRight.RESTRICTED:
							activity.accessRight = AccessRight.RESTRICTED;
							show = showActivity(activity.dataId);
							break;
						case AccessRight.NO:
							activity.accessRight = AccessRight.NO;
							break;
						default:
							throw new Error("unknown accessright type");
					}
					if(show)
						_activities.push(activity);
				}
			}
			
			if(_login.success)
			{
				displayActivities();
			}
			else
			{
				displayLoginError();
			}
		}
		
		private function showActivity(pDataId:String):Boolean
		{
			var result:Boolean = false;
			for each( var connection:XML in _xmlData..connections.*.( @end == pDataId ))
			{
				if( _xmlData..activities.*.( @id == connection.@start).@finalized ||
					 _xmlData..decisions.*.( @id == connection.@start).@finalized)
				{
					result = true;
				} 
			}
			
			return result;
			
		}
		
		private function displayActivities():void
		{
			var mxmlApp:Application = Application(Application.application);
			if(_activities.length > 0)
			{
				if((_activities[0] as ActivityDAO).accessRight == AccessRight.FULL)
				{
					mxmlApp.currentState = "fullAccess";
					var evtObj:AccessEvent = new AccessEvent(AccessEvent.UPDATE, _activities[0] as ActivityDAO)
					dispatchEvent(evtObj);
					//loadFullAccess();	
				}
				else
				{
					mxmlApp.currentState = "restricted";
					loadRestrictedAccess();
				}
			}
			else
			{
				mxmlApp.currentState = "noAccess";
			}
			
			
			invalidateDisplayList();
		}
		
		private function displayLoginError():void
		{
			_lbError.text = "Falsche Login Daten";
			invalidateDisplayList();
		}
		
		private function loadFullAccess():void
		{
			
		}
		
		private function loadRestrictedAccess():void
		{
			
		}
	}
}